package com.laboratory.stomplearning.controller;

import com.laboratory.stomplearning.message.ChatMessage;
import com.laboratory.stomplearning.message.Type;
import lombok.RequiredArgsConstructor;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

import java.security.Principal;
import java.time.Instant;

@Controller
@RequiredArgsConstructor
public class ChatController {

    private final SimpMessagingTemplate simpMessagingTemplate;

    /**
     * 加入房间：客户端 send("/app/room/{roomId}/join", {})
     *
     * @param roomId
     * @param principal
     */
    @MessageMapping("/room/{roomId}/join")
    public void join(@DestinationVariable String roomId, Principal principal) {
        ChatMessage message = ChatMessage.builder()
                .type(Type.JOIN)
                .roomId(roomId)
                .from(principal.getName())
                .content(principal.getName() + "joined")
                .timestamp(Instant.now().toEpochMilli())
                .build();
        simpMessagingTemplate.convertAndSend("/topic/room/" + roomId, message);
    }

    /**
     * 离开房间：客户端 send("/app/room/{roomId}/leave", {})
     *
     * @param roomId
     * @param principal
     */
    @MessageMapping("/room/{roomId}/leave")
    public void leave(@DestinationVariable String roomId, Principal principal) {
        ChatMessage message = ChatMessage.builder()
                .type(Type.LEAVE)
                .roomId(roomId)
                .from(principal.getName())
                .content(principal.getName() + "left")
                .timestamp(Instant.now().toEpochMilli())
                .build();
        simpMessagingTemplate.convertAndSend("/topic/room/" + roomId, message);
    }

    /**
     * 发送消息：客户端 send("/app/room/{roomId}/send", {content:"hi"})
     *
     * @param roomId
     * @param message
     * @param principal
     */
    @MessageMapping("/room/{roomId}/send")
    public void send(@DestinationVariable String roomId, ChatMessage message, Principal principal) {
        ChatMessage chatMessage = ChatMessage.builder()
                .type(Type.CHAT)
                .roomId(roomId)
                .from(principal.getName())
                .content(message.getContent())
                .timestamp(Instant.now().toEpochMilli())
                .build();
        simpMessagingTemplate.convertAndSend("/topic/room/" + roomId, chatMessage);
    }

    /**
     * 私聊：客户端 send("/app/whisper", {to:"user", content:"hi"})
     *
     * @param message
     * @param principal
     */
    @MessageMapping("/whisper")
    public void whisper(ChatMessage message, Principal principal) {
        ChatMessage chatMessage = ChatMessage.builder()
                .type(Type.WHISPER)
                .from(principal.getName())
                .to(message.getTo())
                .content(message.getContent())
                .timestamp(Instant.now().toEpochMilli())
                .build();
        /**
         * 发给单个用户：对方需要订阅 /user/queue/personal
         */
        simpMessagingTemplate.convertAndSendToUser(message.getTo(), "/queue/whisper", chatMessage);
    }

}
